package com.cashmama.market.api.application.controller.user.profile;

import com.alibaba.fastjson.JSONObject;
import com.cashmama.market.api.application.controller.user.profile.request.GpsRequest;
import com.cashmama.market.api.application.controller.user.profile.request.UserPushRiskRequest;
import com.cashmama.market.api.application.controller.user.profile.response.UserCreditResponse;
import com.cashmama.market.api.domain.service.ApiUserService;
import com.cashmama.market.api.domain.vo.RequestLocalInfo;
import com.cashmama.market.api.domain.vo.order.SmsReportInfo;
import com.cashmama.market.api.domain.vo.user.IFSCCheckResponse;
import com.cashmama.market.api.domain.vo.user.profile.*;
import com.cashmama.market.api.infras.handler.ApiResponse;
import com.cashmama.market.api.infras.utils.ZipUtils;
import com.fqgj.common.api.annotations.ParamsValidate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
 * @program: market-api
 * @description 用户认证相关接口
 * @author: frank
 * @create: 2019-12-19 21:21
 **/
@RestController
@RequestMapping("/user/profile")
public class UserProfileController {

    private final static Logger LOGGER = LoggerFactory.getLogger(UserProfileController.class);


    @Autowired
    private ApiUserService apiUserService;
    /**
     * gps保存
     * @param gpsRequest
     * @return
     */
    @RequestMapping(value = "/gps/post", method = RequestMethod.POST)
    public ApiResponse saveGps(@RequestBody @Valid GpsRequest gpsRequest) {

        GpsDTO userGps = new GpsDTO();
        BeanUtils.copyProperties(gpsRequest, userGps);
        apiUserService.saveGps(userGps);
        return new ApiResponse<>("Submit Success");
    }

    /**
     * 保存通讯录
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/contact/post", method = RequestMethod.POST)
    public ApiResponse saveContact(@RequestBody JSONObject jsonObject) {

        apiUserService.saveContact(jsonObject);
        return new ApiResponse<>("Submit success");
    }

    /**
     * 保存短信
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/sms/post", method = RequestMethod.POST)
    public ApiResponse saveSms(@RequestBody JSONObject jsonObject) {
        LOGGER.info("== 上传短信 ==");
        apiUserService.saveSms(jsonObject);
        return new ApiResponse<>("Submit success");
    }

    /**
     * 保存数美信息
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/fingerprint/post", method = RequestMethod.POST)
    public ApiResponse saveFingerprint(@RequestBody JSONObject jsonObject) {
        String userCode = RequestLocalInfo.getCurrentUser().getUserCode();
        String version = RequestLocalInfo.getRequestBasicInfo().getAppVersion();
        String packageName = RequestLocalInfo.getRequestBasicInfo().getPackageName();
        String data = jsonObject.getString("data");
        String gunData = ZipUtils.gunzip(data);
        LOGGER.info("========== 开始保存数美信息 version:{}, packageName:{}, userCode:{}, gunData:{}===========", version, packageName, userCode, gunData);
        FingerprintDTO userProfileFingerprintPostVO = JSONObject.parseObject(gunData, FingerprintDTO.class);
        apiUserService.addShumeiDevice(userCode, userProfileFingerprintPostVO.getFingerprintData());
        LOGGER.info("========== 数美信息保存成功 version:{}, packageName:{}, userCode:{}===========", version, packageName, userCode);
        return new ApiResponse<>("Submit success");
    }

    /**
     * Algo360短信 --客户端sdk结果上报
     * @param jsonObject
     * @return
     */
    @RequestMapping(value = "/algo360/post", method = RequestMethod.POST)
    public ApiResponse Algo360(@RequestBody JSONObject jsonObject) {
        apiUserService.saveAlgo360(jsonObject);
        return new ApiResponse<>("Submit success");
    }

    /**
     * 用户授信
     * @return
     */
    @RequestMapping(value = "/credit", method = RequestMethod.POST)
    public ApiResponse<UserCreditResponse> credit() {
        UserCreditVO creditVO = apiUserService.userCredit();
        return new ApiResponse<>(new UserCreditResponse(creditVO));
    }

    /**
     * 短信报告
     * @return
     */
    @RequestMapping(value = "/sms/report", method = RequestMethod.POST)
    public ApiResponse<SmsReportInfo> report() {
        SmsReportInfo smsReportInfo = apiUserService.report();
        return new ApiResponse<>(smsReportInfo);
    }

    /**
     * 根据ifscCode查询银行信息
     */
    @RequestMapping(value = "/ifsc")
    public ApiResponse<IFSCCheckResponse> bankCheck(@RequestBody JSONObject jsonObject) {
        return new ApiResponse<>(apiUserService.checkBankIFSC(jsonObject));
    }

    /**
     * 推送风控【第一轮】
     * 认证基本信息和基础数据，包括数美、GPS等
     * @return
     */
    @RequestMapping(value = "/push/risk", method = RequestMethod.POST)
    public ApiResponse<UserPushRiskResult> pushRisk() {
        return new ApiResponse<>(apiUserService.pushRisk());
    }

    /**
     * 轮询第一轮风控结果
     * @param userPushRiskRequest
     * @return
     */
    @ParamsValidate
    @RequestMapping(value = "/risk/result", method = RequestMethod.POST)
    public ApiResponse<UserQueryRiskResult> riskResult(@RequestBody @Valid UserPushRiskRequest userPushRiskRequest) {
        return new ApiResponse<>(apiUserService.riskResult(userPushRiskRequest.getRiskNo()));
    }
}